package com.wolves.career.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wolves.career.entity.JobOpportunityPosition;
import com.wolves.career.service.JobOpportunityPositionService;
import com.wolves.career.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 麦翔盛
 * @since 2021-10-23
 */
@RestController
@RequestMapping("/job/position")
@CrossOrigin
public class JobOpportunityPositionController {
    @Autowired
    JobOpportunityPositionService positionService;

    /**
     * 根据id获取职位
     * @param positionId
     * @return
     */
    @GetMapping("/getPosition/{positionId}")
    public R getPosition(@PathVariable Long positionId){
        JobOpportunityPosition position = positionService.getById(positionId);
        return R.ok().data("position",position);
    }

    /**
     * 添加工作职位
     *
     * @param job 工作职位类
     * @return
     */
    @PostMapping("/addPosition")
    public R addPosition(@RequestBody(required = false) JobOpportunityPosition job) {
        boolean save = positionService.save(job);
        if (!save) {
            return R.error().message("插入工作失败").data("success", false);
        }
        return R.ok().data("success", true);
    }

    /**
     * 根据职位id删除职位
     *
     * @param positionId 职位id
     * @return
     */
    @DeleteMapping("/deletePosition/{positionId}")
    public R removePosition(@PathVariable Long positionId) {
        boolean remove = positionService.removeById(positionId);
        if (!remove) {
            return R.error().message("删除失败").data("success", false);
        }
        return R.ok().data("success", true);
    }

    /**
     * 更新职位
     *
     * @param position 职位类
     * @return
     */
    @PutMapping("/updatePosition")
    public R updatePosition(@RequestBody(required = true) JobOpportunityPosition position) {
        boolean update = positionService.updateById(position);
        if (!update) {
            return R.error().message("插入失败").data("success", false);
        }
        return R.ok().data("success", true);
    }

    /**
     * 分页查询职位
     * @param position 职位封装信息
     * @param pageNo 页码
     * @param limit 每页限制数量
     * @return
     */
    @GetMapping("/listPosition/{pageNo}/{limit}")
    public R listPosition(@RequestBody(required = false) JobOpportunityPosition position,
                          @PathVariable long pageNo,
                          @PathVariable long limit) {
        QueryWrapper<JobOpportunityPosition> queryWrapper =
                positionService.queryJobPosition(position);
        Page<JobOpportunityPosition> page = new Page<>(pageNo, limit);
        IPage<JobOpportunityPosition> positionPage = positionService.page(page, queryWrapper);
        List<JobOpportunityPosition> records = positionPage.getRecords();
        long total = positionPage.getTotal();
        return R.ok().data("records",records).data("total",total);
    }


}

